package com.cn.longdistancebusstation.httpHelper;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;


import com.cn.longdistancebusstation.httpapi.WebService;
import com.cn.longdistancebusstation.model.BusInfo;
import com.cn.longdistancebusstation.model.Station;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ParseXMLActivity extends AppCompatActivity {

    private List<Station> mStationList;
    private List<BusInfo> mBusInfolist;
    private String mFlowBusSendTimeDesc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mStationList = new ArrayList<>();
        mBusInfolist = new ArrayList<>();
        //
//        getDesPorts();
//        getBusList();
        getFlowBusSendTimeDesc();
    }

    /*
    * 获取本地几个车站的信息
    * */
    private void getData(){
        Retrofit retrofit = new HttpHelper.WebServiceBuilder().build().getRetrofit();
        WebService webService = retrofit.create(WebService.class);
        Call<String> call = webService.getStations();
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try
                {
                    parseXML(response.body());
                }
                catch(XmlPullParserException e)
                {}
                catch(IOException e)
                {}

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }
    /*
   * 获取济南本地几个车站的信息，已写死在代码中，可以不用调用
   * */
    private void parseXML(String xmlText) throws XmlPullParserException,IOException {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput( new StringReader( xmlText) );
        int eventType = xpp.getEventType();
        Station station = new Station();
        String strTag = "";
        while (eventType != XmlPullParser.END_DOCUMENT)
        {
            if(eventType == XmlPullParser.START_DOCUMENT)
            {
            }
            else if(eventType == XmlPullParser.START_TAG)
            {
                strTag = xpp.getName();
                if(xpp.getName().equals("busStationList"))
                {
                    station = new Station();
                }
            }
            else if(eventType == XmlPullParser.END_TAG)
            {
                strTag = "";
                if(xpp.getName().equals("busStationList"))
                {
                    mStationList.add(station);
                }
            }
            else if(eventType == XmlPullParser.TEXT)
            {
                if(strTag.equals("StationID"))
                {
                    station.setStationID(xpp.getText());
                }
                else if(strTag.equals("StationName"))
                {
                    station.setStationName(xpp.getText());
                }
            }
            eventType = xpp.next();
        }
    }

    /*
    * 获取所有目的站点
    * */
    private void getDesPorts(){
        Retrofit retrofit = new HttpHelper.WebServiceBuilder().build().getRetrofit();
        WebService webService = retrofit.create(WebService.class);
        Call<String> call = webService.getDesPorts();
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try
                {
                    Log.i("ParseXMLActivity", response.body());
                    parseXMLForDesPorts(response.body());
                }
                catch(XmlPullParserException e)
                {}
                catch(IOException e)
                {}
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

    }

    private void parseXMLForDesPorts(String xmlText) throws XmlPullParserException,IOException {
        mStationList.clear();
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xmlPullParser = factory.newPullParser();
        xmlPullParser.setInput(new StringReader(xmlText));
        int eventType = xmlPullParser.getEventType();
        String strTagName = "";
        Station station = null;
        while(eventType != XmlPullParser.END_DOCUMENT){
            if(eventType == XmlPullParser.START_DOCUMENT)
            {}
            else if(eventType == XmlPullParser.START_TAG)
            {
                strTagName = xmlPullParser.getName();
                if(strTagName.equals("portList"))
                {
                    station = new Station();
                }
            }
            else if(eventType == XmlPullParser.END_TAG)
            {
                strTagName = "";
                if(xmlPullParser.getName().equals("portList"))
                {
                    mStationList.add(station);
                }
            }
            else if(eventType == XmlPullParser.TEXT)
            {
                if(strTagName.equals("PortID"))
                {
                    station.setStationID(xmlPullParser.getText());
                }
                else if(strTagName.equals("PortName"))
                {
                    station.setStationName(xmlPullParser.getText());
                }
                else if(strTagName.equals("PortCode"))
                {
                    station.setStationCode(xmlPullParser.getText());
                }
            }
            eventType = xmlPullParser.next();
        }
    }

    /*
    * 获取车次列表
    * */
    private void getBusList(){
        Retrofit retrofit = new HttpHelper.WebServiceBuilder().build().getRetrofit();
        WebService webService = retrofit.create(WebService.class);
        Date today = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strToday = dateFormat.format(today);
        Call<String> call = webService.getBusList("1", "2018-02-01", dateFormat.format(today));
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try
                {
                    parseXMLForBusList(response.body());
                }
                catch(XmlPullParserException e)
                {}
                catch(IOException e)
                {}
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    private void parseXMLForBusList(String xmlText) throws XmlPullParserException, IOException {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xmlPullParser = factory.newPullParser();
        xmlPullParser.setInput(new StringReader(xmlText));
        int eventType = xmlPullParser.getEventType();
        BusInfo busInfo = null;
        String strTagName = "";
        String strTagText = "";
        Boolean isShow = false;
        while(eventType != XmlPullParser.END_DOCUMENT)
        {
            if(eventType == XmlPullParser.START_DOCUMENT)
            {}
            else if(eventType == XmlPullParser.START_TAG)
            {
                strTagName = xmlPullParser.getName();
                if(strTagName.equals("MobilportBliidListHaveDiscount"))
                {
                    busInfo = new BusInfo();
                }
            }
            else if(eventType == XmlPullParser.END_TAG)
            {
                strTagName = "";
                if(xmlPullParser.getName().equals("MobilportBliidListHaveDiscount") && isShow)
                {
                    mBusInfolist.add(busInfo);
                }
            }
            else if(eventType == XmlPullParser.TEXT)
            {
                strTagText = xmlPullParser.getText();
                if(strTagName.equals("SendStationName"))
                {
                    busInfo.setStartStationName(strTagText);
                }
                else if(strTagName.equals("PortName"))
                {
                    busInfo.setEndStationName(strTagText);
                }
                else if(strTagName.equals("BusKindName"))
                {
                    busInfo.setBusType(strTagText);
                }
                else if(strTagName.equals("BliidIsCanSellTicket"))
                {
                    if(strTagText.equals("不可售")) isShow = false;
                    else isShow = true;
                }
                else if(strTagName.equals("Bliid"))
                {
                    busInfo.setBusNo(strTagText);
                }
                else if(strTagName.equals("BliidSendTime"))
                {
                    busInfo.setSendTime(strTagText);
                }
                else if(strTagName.equals("BliidCheckWindowID"))
                {
                    busInfo.setCheckWindowID(strTagText);
                }
                else if(strTagName.equals("PortPrice"))
                {
                    busInfo.setTicketPrice(strTagText);
                }
                else if(strTagName.equals("BliidCanSellNum"))
                {
                    busInfo.setLeftTickets(Integer.parseInt(strTagText));
                }
                else if(strTagName.equals("BliidIsFlow"))
                {
                    busInfo.setFlow(strTagText.equals("0")?false:true);
                }
                else if(strTagName.equals("BliidStartNum"))
                {
                    busInfo.setStartSeatNo(strTagText);
                }
            }
            eventType = xmlPullParser.next();
        }
    }

    /*
    * 获取流水车的发车时间段
    * */
    private void getFlowBusSendTimeDesc() {
        Retrofit retrofit = new HttpHelper.WebServiceBuilder().build().getRetrofit();
        WebService webService = retrofit.create(WebService.class);
        Call<String> call = webService.getFlowBusSendTimeDesc("GHZZ", "2018-02-01");
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try
                {
                    mFlowBusSendTimeDesc = parseXMLForFlowBusSendTimeDesc(response.body());
                }
                catch(XmlPullParserException e)
                {}
                catch (IOException e)
                {}
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    private String parseXMLForFlowBusSendTimeDesc(String xmlText) throws XmlPullParserException, IOException {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xmlPullParser = factory.newPullParser();
        xmlPullParser.setInput(new StringReader(xmlText));
        int eventType = xmlPullParser.getEventType();
        String strTagName = "";
        String strDesc = "";
        while(eventType != XmlPullParser.END_DOCUMENT)
        {
            if(eventType == XmlPullParser.START_DOCUMENT)
            {}
            else if(eventType == XmlPullParser.START_TAG)
            {
                strTagName = xmlPullParser.getName();
            }
            else if(eventType == XmlPullParser.END_TAG)
            {
                strTagName = "";
            }
            else if(eventType == XmlPullParser.TEXT)
            {
                if(strTagName.equals("string")) strDesc = xmlPullParser.getText();
            }
            eventType = xmlPullParser.next();
        }
        return strDesc;
    }
}
